
#include "MultiBrokerStat.h"

namespace detail {

MultiBrokerStat::MultiBrokerStat() : m_dirty(true), m_enable(false) {}

MultiBrokerStat::~MultiBrokerStat() {}

void MultiBrokerStat::AddBroker(const char* bondKey, const char* market) {
  if (!m_enable) return;
  AddBrokerImpl(bondKey, market);
}

void MultiBrokerStat::RemoveBroker(const char* bondKey, const char* market) {
  if (!m_enable) return;
  RemoveBrokerImpl(bondKey, market);
}

void MultiBrokerStat::AddBrokerImpl(const char* bondKey, const char* market) {
  std::string combBondKey;
  combBondKey.reserve(256);
  combBondKey += bondKey;
  combBondKey += ".";
  combBondKey += market;
  CompanyIdCountMap::iterator it = m_broker_stat.find(combBondKey);
  if (it == m_broker_stat.end())
    m_broker_stat.insert(CompanyIdCountMap::value_type(combBondKey, 1));
  else
    it->second++;
}

void MultiBrokerStat::RemoveBrokerImpl(const char* bondKey,
                                       const char* market) {
  std::string combBondKey;
  combBondKey.reserve(256);
  combBondKey += bondKey;
  combBondKey += ".";
  combBondKey += market;
  CompanyIdCountMap::iterator it = m_broker_stat.find(combBondKey);
  if (it != m_broker_stat.end()) {
    it->second--;
    if (it->second <= 0) m_broker_stat.erase(it);
  }
}
void MultiBrokerStat::Clear(bool enable) {
  m_enable = enable;
  m_dirty = true;
  m_broker_stat.clear();
}

}  // namespace detail